package com.hungryinlindholmen;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SearchServlet
 * 
 * SearchServlet creates a SearchResult-object and puts it in the request, then
 * redirects to "searchresults.jsp".
 * 
 * @author Olof Ekelund.
 */

@WebServlet(urlPatterns = "/SearchServlet")
public class SearchServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
	String searchString = request.getParameter("Search");
	String types[] = request.getParameterValues("foodtype");
	String meal = request.getParameter("meal");
	String weekday = request.getParameter("weekday");
	int maxPrice = 5000;
	try {
	    maxPrice = Integer.parseInt(request.getParameter("price"));
	} catch (Exception e) {
	}

	SearchResult sR = new SearchResult(searchString, types, meal, weekday,
		maxPrice);
	sR.updateTable();

	request.setAttribute("SearchResult", sR);
	request.getSession().setAttribute("SearchResult", sR);
	request.getSession().setAttribute("pages", sR.getPages());

	request.getSession().setAttribute("DBConnection", sR.getCon());
	RequestDispatcher dispatcher = request
		.getRequestDispatcher("searchresults.jsp");
	dispatcher.forward(request, response);
	return;
    }
}